package org.jeecg.modules.demo.iaijbill.service.impl;

import org.jeecg.modules.demo.iaijbill.entity.IaIjbill;
import org.jeecg.modules.demo.iaijbill.entity.IaIjbillB;
import org.jeecg.modules.demo.iaijbill.mapper.IaIjbillBMapper;
import org.jeecg.modules.demo.iaijbill.mapper.IaIjbillMapper;
import org.jeecg.modules.demo.iaijbill.service.IIaIjbillService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 调拨单
 * @Author: jeecg-boot
 * @Date:   2024-03-12
 * @Version: V1.0
 */
@Service
public class IaIjbillServiceImpl extends ServiceImpl<IaIjbillMapper, IaIjbill> implements IIaIjbillService {

	@Autowired
	private IaIjbillMapper iaIjbillMapper;
	@Autowired
	private IaIjbillBMapper iaIjbillBMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(IaIjbill iaIjbill, List<IaIjbillB> iaIjbillBList) {
		iaIjbillMapper.insert(iaIjbill);
		if(iaIjbillBList!=null && iaIjbillBList.size()>0) {
			for(IaIjbillB entity:iaIjbillBList) {
				//外键设置
				entity.setIdH(iaIjbill.getId());
				iaIjbillBMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(IaIjbill iaIjbill,List<IaIjbillB> iaIjbillBList) {
		iaIjbillMapper.updateById(iaIjbill);
		
		//1.先删除子表数据
		iaIjbillBMapper.deleteByMainId(iaIjbill.getId());
		
		//2.子表数据重新插入
		if(iaIjbillBList!=null && iaIjbillBList.size()>0) {
			for(IaIjbillB entity:iaIjbillBList) {
				//外键设置
				entity.setIdH(iaIjbill.getId());
				iaIjbillBMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		iaIjbillBMapper.deleteByMainId(id);
		iaIjbillMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			iaIjbillBMapper.deleteByMainId(id.toString());
			iaIjbillMapper.deleteById(id);
		}
	}
	
}
